<?php
/**
 * Created by Sublime.
 * Author: lingyun
 * Date: 2017-12-06 20:33:49
 * @desc  支付宝支付类
 */
namespace app\home\controller;
use think\Controller;
use think\Config;
use think\Model;
use think\Db;
use think\Request;
use alipay\Pagepay;
use app\comm\service\AlipayService;
class Alipay extends Controller{
	/**
	 * 支付宝支付参数
	 * @Author   lingyun
	 * @DateTime 2017-12-06T20:36:45+0800
	 * @return   [type]                   [description]
	 */
	public function pay(){
		$post = input('param.');			//接受的参数
		$this->checkorder($post);		//检验订单状态
		$params = array(
			'out_trade_no'=>$post['order_sn'],
			'subject'=>'订单支付',
			'total_amount'=>$post['final_amount']
		);
		$config = config('alipay');     //修改支付宝的回调url地址
        $config['notify_url'] = "http://ysbl.fxshe.com/index.php/index/notify/notifyalipay";
		Pagepay::pay($params,$config);
	}
	/**
	 * 回调地址
	 * @Author   lingyun
	 * @DateTime 2017-12-12T16:17:17+0800
	 * @return   [type]                   [description]
	 */
	public function notify(){
		$request = input('post.');
		$dir = 'public/notify';
		if (!file_exists($dir)){
            mkdir ($dir,0777,true);
        }
        $json = time().'.json';
		file_put_contents("1.json", $dir.'/'.$json);
		$service = new AlipayService();
		$result = $service->init();
		if($result['code'] == 1){
			$this->success('支付成功',url('Index/index'));
		}else{
			$this->error($result['msg'],url('Index/index'));
		}
	}

    /**
     * @Author lignyun
     * @Desc    会员充值 -- 支付宝
     */
	public function recharge(){
        $post = input('param.');
        $post['final_amount'] = $post['amount'];
        $data = array(
            'uid'=>$post['uid'],
            'admin_id'=>0,
            'store_id'=>0,
            'order_id'=>0,
            'order_sn'=>$post['order_sn'],
            'pay_status'=>0,
            'pay_type'=>$post['pay_type'],
            'amount'=>$post['amount'],
            'sign'=>2,
            'chargetype'=>1,
            'charge_cn'=>"会员充值",
            'addtime'=>time()
        );
        Db::name('user_wallet_log')->insert($data);
        $result = $this->checkRechargeOrder($post);
        if ($result["status"] == 0) {
            $this->error($result["msg"]);
        }
        $post = input('param.');			//接受的参数
        $this->checkorder($post);		//检验订单状态
        $params = array(
            'out_trade_no'=>$post['order_sn'],
            'subject'=>'订单支付',
            'total_amount'=>$post['final_amount']
        );
        $config = config('alipay');     //修改支付宝的回调url地址
        $config['notify_url'] = "http://ysbl.fxshe.com/index.php/index/notify/notifyalirecharge";
        Pagepay::pay($params);
    }

	/**
	 * 检查订单信息
	 * @Author   lingyun
	 * @DateTime 2017-12-12T19:59:54+0800
	 *
	 */
	private function checkorder($param){
		empty($param['order_sn']) && $this->error('订单不存在',url('Index/index'));
		$order = Db::name('order')->where('order_sn',$param['order_sn'])->find();
		if($order['pay_status'] == 1){
			$this->error('该订单已经支付，请勿重复支付',url('Index/index'));
		}else{
			$cur_time = time()-30*60;
			if($order['addtime']<$cur_time){
				$this->error('订单已过期，不能发起支付',url('Index/index'));
			}
		}
	}

    /**
     * @Author lignyun
     * @Desc    检查会员充值订单
     */
	private function checkRechargeOrder(){
        if(empty($post['order_sn'])){
            return ['status'=>0,'msg'=>'找不到支付订单,请重新充值'];
        }else{
            $order = Db::name('user_wallet_log')->where("order_sn",$post['order_sn'])->find();
            if(empty($order)){
                return ['status'=>0,'msg'=>'找不到支付信息,请重新再试'];
            }else if($order['pay_status'] == 1){
                return ['status'=>0,'msg'=>'该订单已支付，请勿重复支付'];
            }else if(time()-3600>$order["addtime"]){
                return ['status'=>0,'msg'=>'该订单已过期，不能进行支付'];
            }
        }
        return ['status'=>1,'msg'=>'找到订单'];
    }
}
